package com.sx.pipeline.ui.advertise

import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.AdvertisingAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActAdvertisingBinding
import com.sx.pipeline.repos.http.res.AdvertiseVO


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 广告发布
 */
@Route(path = RoutePath.Main.ADVERTISING)
class AdvertisingActivity :
    CYDataBindingBaseActivity<ActAdvertisingBinding,AdvertisingVM>(R.layout.act_advertising),
    View.OnClickListener {

    private lateinit var mAdapter: AdvertisingAdapter

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }

        // 获取广告宣传列表
        mViewModel.getAdvertiseList(mPage)
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.getAdvertiseList(mPage)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.getAdvertiseList(mPage)
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {

        }
    }

    override fun initViewObservable() {
        super.initViewObservable()

        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        // 广告宣传列表
        setAdvertisingList()

    }

    private fun setAdvertisingList() {
        // 广告宣传列表
        observe(mViewModel.advertisingListEvent) {
            if (it != null && !it.records.isNullOrEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                mBinding.recyclerView.visibility=View.VISIBLE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        this, LinearLayoutManager.VERTICAL, false)
                    mAdapter = AdvertisingAdapter(it.records as MutableList<AdvertiseVO>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->
                        val info = adapter.data[position] as AdvertiseVO
                        RouteManager.Main.intentToAdvertisingGoods(info.id)
                    }
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<AdvertiseVO>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility=View.GONE
            }
        }
    }
}